package com.neusoft.bigdata.hbase.util;

import java.io.IOException;
import java.util.Iterator;

import org.apache.hadoop.hbase.client.HTable;
import org.apache.hadoop.hbase.client.Result;
import org.apache.hadoop.hbase.client.ResultScanner;
import org.apache.hadoop.hbase.client.Scan;
import org.apache.hadoop.hbase.util.Bytes;

/**
 * HBase数据构造工具类
 * 
 * @author lihzh
 * @date 2012-11-16 上午11:04:22
 */
public class HBaseDataUtil {

	public static Iterator<Result> getByFQNAndColumns(HTable hTable,
			String fqn, String columnFamily, long startTime, long timeRange,
			String... columns) throws IOException {
		Scan scan = new Scan();
		scan.setStartRow(Bytes.toBytes(fqn + "_" + startTime));
		scan.setStopRow(Bytes.toBytes(fqn + "_" + (startTime + timeRange)));
		for (String column : columns) {
			scan.addColumn(Bytes.toBytes(columnFamily), Bytes.toBytes(column));
		}
		ResultScanner scanner = hTable.getScanner(scan);
		return scanner.iterator();
	}

	public static Iterator<Result> getByFQN(HTable hTable, String fqn,
			long startTime, long timeRange) throws IOException {
		Scan scan = new Scan();
		scan.setStartRow(Bytes.toBytes(fqn + "_" + startTime));
		scan.setStopRow(Bytes.toBytes(fqn + "_" + (startTime + timeRange)));
		scan.setCaching(10000);
		ResultScanner scanner = hTable.getScanner(scan);
		return scanner.iterator();
	}
}
